﻿using LxCore;

namespace LxCore.Samples
{
    public class TransientHeatConduction : SampleVMBase
    {
        public TransientHeatConduction() : base()
        {
            Name = "瞬态导热";
            FileName = "Case1-2Zone4w.msh";
        }
        public override void RunCase()
        {
            ReadFile();
            //create physical model
            var sim = LxSim.Sim;
            var physical_1 = LxSim.Sim.Physics.CreatePhysicalModel();
            physical_1.IsEnergyEnabled = true;

            var physical_2 = LxSim.Sim.Physics.CreatePhysicalModel();
            physical_2.IsEnergyEnabled = true;
            physical_2.MaterialProperty.ThermalConductivity = new ConstField.Double(400);

            //2.设置区域属性及边界条件
            var zone_1 = FindZone("BODY1");
            ResHelper.GetCond(zone_1).PhysicalModel = physical_1;
            var thread_in = FindBound(zone_1, "IN");
            var threadinset = ResHelper.GetCond(thread_in);
            threadinset.WallThermalType = WallThermalTypes.定热流密度;
            threadinset.HeatFlux_b = new ConstField.Double(1000);

            var zone_2 = FindZone("BODY2");
            ResHelper.GetCond(zone_2).PhysicalModel = physical_2;
            var thread_out = FindBound(zone_2, "OUT");
            var threadoutset = ResHelper.GetCond(thread_out);
            threadoutset.WallThermalType = WallThermalTypes.定壁温;
            threadoutset.Temperature = new ConstField.Double(300);

            var joint = FindJoint("CONTACT");
            var jointset = ResHelper.GetCond(joint);
            jointset.IsEnabled = true;
            jointset.ThermalResistance = new ConstField.Double(0.005);//0.005
            jointset.EnergySource_b = new ConstField.Double(800);

            var monitor = LxSim.Sim.Monitors.CreateMonitor("IN面温度", StatisticTypes.AreaAverage);
            monitor.ScalarType = ScalarFieldTypes.Temperature;
            monitor.SetSelector(thread_in, true);
            monitor = LxSim.Sim.Monitors.CreateMonitor("Con1温度", StatisticTypes.AreaAverage);
            monitor.ScalarType = ScalarFieldTypes.Temperature;
            monitor.SetSelector(joint.NBound, true);
            monitor = LxSim.Sim.Monitors.CreateMonitor("Con2温度", StatisticTypes.AreaAverage);
            monitor.ScalarType = ScalarFieldTypes.Temperature;
            monitor.SetSelector(joint.OBound, true);

            var graph = LxSim.Sim.Visuals.CreateGraph("端面温度云图");
            graph.GraphType = GraphTypes.等值图;
            graph.ScalarType = ScalarFieldTypes.Temperature;
            graph.SetSelector(thread_in, true);
            graph.SetSelector(thread_out, true);

            //3.设置计算参数进行计算                
            LxSim.Sim.Steps.IterationStep.MaxStepRule.MaxStep = 10;
            LxSim.Sim.Physics.TimeDiscreteType = TimeDiscreteTypes.Implicit_2nd;
            LxSim.Sim.Physics.TimeInterval = new ConstField.Double(7200);
            LxSim.Sim.Steps.TimeStep.MaxStepRule.MaxStep = 20;

            RunSimulation();
        }
    }
}
